package com.xjrsoft.module.customerTwo.EMP.electricity_cost_records_day.service.impl;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.customerTwo.EMP.electricity_cost_records_day.entity.TElectricityCostRecordsDay;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xjrsoft.core.tool.utils.CollectionUtil;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import com.xjrsoft.module.customerTwo.EMP.electricity_cost_records_day.dto.TElectricityCostRecordsDayListDto;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import java.util.List;
import com.xjrsoft.module.customerTwo.EMP.electricity_cost_records_day.mapper.TElectricityCostRecordsDayMapper;
import com.xjrsoft.module.customerTwo.EMP.electricity_cost_records_day.service.ITElectricityCostRecordsDayService;
import com.xjrsoft.core.mp.base.BaseService;
import com.xjrsoft.core.tool.utils.Func;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 *  服务实现类
 *
 * @author dongxinhang
 * @since 2023-07-24
 */
@Service
@AllArgsConstructor
public class TElectricityCostRecordsDayServiceImpl extends BaseService<TElectricityCostRecordsDayMapper, TElectricityCostRecordsDay> implements ITElectricityCostRecordsDayService {

	@Override
	public IPage<TElectricityCostRecordsDay> getPageList(TElectricityCostRecordsDayListDto pageListDto) {
		//处理起始日期
		String start = pageListDto.getYear()+"-"+pageListDto.getMonth()+"-01";
		if(pageListDto.getMonth().length() == 1){
			start = pageListDto.getYear()+"-0"+pageListDto.getMonth()+"-01";
		}
		DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		LocalDate dateParam = LocalDate.parse(start, df);
		Wrapper<TElectricityCostRecordsDay> wrapper = null;
		if(StringUtils.isNotBlank(pageListDto.getIdentifyId())){
			wrapper = Wrappers.<TElectricityCostRecordsDay>query().lambda()
					.eq(!StringUtil.isEmpty(pageListDto.getIdentifyId()), TElectricityCostRecordsDay::getIdentifyId, pageListDto.getIdentifyId())
					.eq(!StringUtil.isEmpty(pageListDto.getRoomNumber()), TElectricityCostRecordsDay::getRoomNumber, pageListDto.getRoomNumber())
					.ge(!StringUtil.isEmpty(dateParam), TElectricityCostRecordsDay::getDate, dateParam)
					.le(!StringUtil.isEmpty(dateParam.with(TemporalAdjusters.lastDayOfMonth())), TElectricityCostRecordsDay::getDate,dateParam.with(TemporalAdjusters.lastDayOfMonth()));
		}else{
			wrapper = Wrappers.<TElectricityCostRecordsDay>query().lambda()
					.isNull(TElectricityCostRecordsDay::getIdentifyId)
					.eq(!StringUtil.isEmpty(pageListDto.getRoomNumber()), TElectricityCostRecordsDay::getRoomNumber, pageListDto.getRoomNumber())
					.ge(!StringUtil.isEmpty(dateParam), TElectricityCostRecordsDay::getDate, dateParam)
					.le(!StringUtil.isEmpty(dateParam.with(TemporalAdjusters.lastDayOfMonth())), TElectricityCostRecordsDay::getDate,dateParam.with(TemporalAdjusters.lastDayOfMonth()));
		}
		return this.page(ConventPage.getPage(pageListDto), wrapper);
	}

	@Override
	public boolean dealElectricityCostRecordsDay(String satrt, String end, BigDecimal electricityPrice) {
		//根据电量汇总新增每日电量记录
		int count =  this.baseMapper.saveDataByEleSum(satrt,end,electricityPrice);
		if(count > 0){
			//获取每日电量记录表里需要分摊的记录
			List<String> fidList = this.baseMapper.selectProportionIdList(satrt,end);
			if(CollectionUtil.isNotEmpty(fidList)){
				//修改每日电量记录表里需要分摊的记录 电量数据
				count = this.baseMapper.updateProportionData(fidList);
				if(count == 0){
					return false;
				}
			}
		}
		return true;
	}


}